package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.query.DestinationQuery;
import cn.wolfcode.wolf2w.repository.DestinationRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IRegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

@Service
public class DestinationServiceImpl implements IDestinationService {

    @Autowired
    private DestinationRepository repository ;
    @Autowired
    private IRegionService regionService ;
    @Autowired
    private MongoTemplate template;
    @Autowired
    private IDestinationService destinationService ;
    @Override
    public void save(Destination destination) {
        repository.save(destination);
    }

    @Override
    public void update(Destination destination) {
        repository.save(destination);
    }

    @Override
    public void delete(String id) {
        repository.deleteById(id);
    }

    @Override
    public Destination get(String id) {
        return repository.findById(id).orElse(null);
    }

    @Override
    public List<Destination> list() {
        return repository.findAll();
    }

    @Override
    public void changeHotValue(String id, int hot) {
        /*Region region = regionService.get(id);
        region.setIshot(hot);
        regionService.update(region);
        */
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(id));
        Update update = new Update();
        update.set("ishot",hot );
        template.updateMulti(query,update ,Region.class );
    }

    @Override
    public List<Destination> queryDestByRegionId(String rid) {
        Region region = regionService.get(rid);
        List<String> ids = region.getRefIds();
        List<Destination> list = repository.findByIdIn(ids);
        return list;
    }

    @Override
    public Page<Destination> query(DestinationQuery qo) {
       Query query = new Query();
       if(StringUtils.hasLength(qo.getKeyword())){
           query.addCriteria(Criteria.where("name").regex(qo.getKeyword()));
       }
     if(StringUtils.hasLength(qo.getParentId())){
            query.addCriteria(Criteria.where("parentId").is(qo.getParentId()));
        }else{
         query.addCriteria(Criteria.where("parentId").is(null));
       }

        long count = template.count(query, Destination.class);
        if(count==0){
            Page.empty();
        }
        if(count%qo.getPageSize()==0&&qo.getCurrentPage()>1){
            qo.setCurrentPage(qo.getCurrentPage()-1);
        }
        Pageable pageable = PageRequest.of(qo.getCurrentPage()-1,qo.getPageSize() , Sort.Direction.ASC ,"_id");
        query.with(pageable);

        List<Destination> list = template.find(query, Destination.class);
        return new PageImpl<>(list,pageable ,count );
    }

    @Override
    public List<Destination>  getToasts(String parentId) {
        List<Destination> list = new ArrayList<>();
        while(StringUtils.hasLength(parentId)){
            Destination destination = this.get(parentId);
            list.add(destination);
            parentId = destination.getParentId();
        }
        Collections.reverse(list);
        return list ;
    }
        /*if(!StringUtils.hasLength(parentId)){
            return Collections.emptyList();
        }
        Destination dest = template.findById(parentId, Destination.class);
        list.add(dest);
        while(dest.getParentId()!=null){
            dest = template.findById(dest.getParentId(), Destination.class);
            list.add(dest);
        }
        Collections.reverse(list);
        return list;
    }*/
        /*List<Destination> list = new ArrayList<>();
        if(!StringUtils.hasLength(parentId)){
           return Collections.emptyList();
        }
        creatToast(list,parentId );
        Collections.reverse(list);
        return list;
    }
    public void creatToast(List<Destination> list ,String parentId){
        Destination dest = template.findById(parentId, Destination.class);
        list.add(dest);
        if(StringUtils.hasLength(dest.getParentId())){
            creatToast(list,dest.getParentId() );
        }
    }*/

    @Override
    public void updateInfo(String id, String info) {
        Destination dest = repository.findById(id).orElse(null);
        dest.setInfo(info);
        destinationService.update(dest);
    }

    @Override
    public List<Destination> getDestByRegionId(String regionId) {
        List<Destination> list = null ;
        if("-1".equals(regionId)){
            list = repository.findByParentName("中国");
        }else {
            /*Region region = regionService.get(regionId);
            List<String> refIds = region.getRefIds();
            list= repository.findByIdIn(refIds);*/
             list = this.queryDestByRegionId(regionId);
        }
           for(Destination dest : list){
            Pageable pageable = PageRequest.of(0, 8, Sort.Direction.ASC ,"_id");
               List<Destination> dest1 = repository.findByParentId(dest.getId(),pageable);
               dest.setChildren(dest1);
           }
        return list;
    }

    @Override
    public Destination getByName(String keyword) {
        return repository.findByName(keyword);
    }
}
